package com.cxg.fairy.ec.main.order;

import android.os.Handler;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ec.main.personal.favor.FavorItemFields;
import com.cxg.fairy.ec.main.personal.favor.FavorItemType;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.diabin.latte.ec.R;

import java.math.BigDecimal;
import java.util.List;


/**
 * Created by Administrator on 2018/3/8.
 */

public class PostOrderGoodsListAdapter extends MultipleRecyclerAdapter {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    public PostOrderGoodsListAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(66,R.layout.item_post_order_goods);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        switch (holder.getItemViewType()) {
            case 66:
                final String title = entity.getField(FavorItemFields.GOODS_NAME);
                final String unit = entity.getField(FavorItemFields.GOODS_UNIT);
                final BigDecimal price = entity.getField(FavorItemFields.GOODS_PRICE);
                final String img = entity.getField(FavorItemFields.IMAGE_URL);
                final int cartQuantity = entity.getField(FavorItemFields.GOODS_COUNT);

                final AppCompatTextView favorTitle = holder.getView(R.id.tv_favor_item_title);
                final AppCompatTextView favorUnit = holder.getView(R.id.tv_favor_item_unit);
                final AppCompatTextView favorPrice = holder.getView(R.id.tv_favor_item_price);
                final AppCompatImageView favorImg = holder.getView(R.id.image_favor_item);
                final AppCompatTextView actGoodsCount = holder.getView(R.id.itv_favor_left_slide);

                favorTitle.setText(title);
                favorUnit.setText(unit);
                favorPrice.setText(price+"");
                Glide.with(holder.itemView.getContext()).load(img).apply(OPTIONS).into(favorImg);
                actGoodsCount.setText("x"+cartQuantity);

                break;
            default:
                break;
        }
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(FavorItemFields.SPAN_SIZE);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public MultipleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_order_goods, parent, false);
        return new MultipleViewHolder(root);
    }
}
